package file;

import java.io.File;

public class DeleteFile {
	public static void main(String[] args) {
		// File file = new File("d:/b");
		// getVideo(file);

		removeAllFiles(new File("D:/kvin"));
	}

	// search video file
	private static void getVideo(File file) {
		File[] myDir = file.listFiles();
		for (File file2 : myDir) {
			if (file2.isDirectory()) {
				getVideo(file2);
			} else {
				for (String s : Constants.VD_SUFFIX) {
					int ind = file2.getName().lastIndexOf(".");
					int end = file2.getName().length();
					if (s.equals(file2.getName().substring(ind + 1, end))) {
						p(file2.getAbsoluteFile());
					}
				}
				// p(file2.getAbsolutePath());
			}

		}
	}

	// delete file
	static void removeAllFiles(File s) {
		if (s == null)
			return;
		while (s.exists()) {
			File[] files = s.listFiles();// get all files
			if (files.length == 0) {
				p(s.getAbsolutePath());
				s.delete();// if the directory is empty then deleted
			} else {
				for (File file : files) {
					if (!file.isDirectory()) {
						p(file.getAbsolutePath());
						file.delete();// delete when it is a file
					} else {
						removeAllFiles(file);
					}

				}
			}
		}
	}

	// simple delete
	static void remove(String path) {
		if (path == null || path.equals(""))
			return;
		File f = new File(path);
		if (f.exists()) {
			if (f.delete())
				p("delete successfully");
		}

	}

	static void p(Object o) {
		System.out.println(o);
	}
}
